package com.zdy.gaoji;

import com.zdy.demo.TestThread2;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by 逸足天涯
 * on 3/9/2020.
 */
public class TestLock {
    public static void main(String[] args) {
        TestLock2 t = new TestLock2();
        new Thread(t).start();
        new Thread(t).start();
        new Thread(t).start();
    }

}
class TestLock2 implements Runnable{
    int tickets = 10;
    private final ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            try {
                lock.lock();
                if (tickets > 0){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(tickets--);
                }else {
                    break;
                }
            }finally {
                lock.unlock();
            }

        }

    }
}